package com.dsbj.mp.icardservice.controller.companybus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.mp.icardservice.controller.BaseController;
import com.dsbj.mp.icardservice.entity.companybus.CompanyBus;
import com.dsbj.mp.icardservice.entity.companybus.Passenger;
import com.dsbj.mp.icardservice.model.MpUser;
import com.dsbj.mp.icardservice.service.companybus.CompanyBusService;
import com.dsbj.mp.icardservice.service.companybus.PassengerService;
import com.dsbj.mp.icardservice.utils.picprocess.Base64ImageUtil;
import com.dsbj.mp.icardservice.utils.picprocess.QrcodeProcess;
import com.dsbj.mp.icardservice.utils.strprocess.ObjectParse;
import com.dsbj.mp.icardservice.utils.strprocess.StrUtil;
import com.dsbj.mp.icardservice.utils.strprocess.Time_convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author cjw 2020-02-25
 */
@RestController
@Transactional
@RequestMapping(value = "/companybus")
public class ContrCompanyBus extends BaseController {
	
	@Resource
	private CompanyBusService companyBusService;
	
	@Resource
	private PassengerService passengerService;
	
	
	
	/**
	 * 根据班车编号busno获取对应的二维码。
	 * @param
	 * @return 
	 * @author cjw 2020-02-25
	 */
	@RequestMapping("/getCompanyBusQrcode")
	public ResponseMessage getCompanyBusQrcode(String busno) throws IOException {
		//验证传入的参数
		try{
			busno= ObjectParse.parse_toStr_elseException(busno);
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!"+e.toString());
		}
		
		//根据busno获取对应的班车信息。
		CompanyBus companyBus= companyBusService.getCompanyBusByBusno(busno);
		if(companyBus==null){
			return ResponseMessage.error("未找到对应的公司班车信息!");
		}
		//根据班车编号busno生成对应的二维码字符串
		String qrcodeStr=QrcodeProcess.generateQrcodeBase64(companyBus.getBusno(),800,0xFF00A650);
		
		/**
		 * 解码"二维码Base64字符串"成字节数组,并存入文件中。
		 */
		/* 部署时需重新设置文件存储目录。
		String filePathPrefix="./dsbj-mp-icard-service/src/main/resources";
		String filePath=filePathPrefix+"/tempPic/companyBusQrcode"+ Time_convert.getCurrentTime_onlyDigit()+".png";
		Base64ImageUtil.generateFileFromBase64Str(qrcodeStr,filePath);
		*/
		
		//返回信息体
		Map<String,Object> mapReturn=new LinkedHashMap<String,Object>();
		mapReturn.put("qrcodeStr",qrcodeStr);
		return ResponseMessage.ok(mapReturn);
	}
	
	/**
	 * 存入乘客乘车信息。
	 * 传入参数: busno
	 * @param
	 * @return
	 * @author cjw 2020-02-25
	 */
	@RequestMapping(value = "/addPassenger")
	public ResponseMessage addPassenger(@RequestBody Map<String,Object> map){
		//解析传来的参数
		MpUser mpUser=(MpUser) request.getAttribute("wxUser");
		String busno=null;
		try{
			busno=ObjectParse.parse_toStr_elseException(map.get("busno"));
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!"+e.toString());
		}
		
		/**
		 * 存入乘客乘车信息。
		 */
		//根据busno获取对应的班车信息。
		CompanyBus companyBus= companyBusService.getCompanyBusByBusno(busno);
		if(companyBus==null){
			return ResponseMessage.error("未找到对应的公司班车信息!");
		}
		
		//存入乘客信息。
		Passenger passenger=new Passenger();
		passenger.setBusno(busno);
		passenger.setIdcardno(mpUser.getIdcardno());
		passenger.setStatus(1);
		passenger.setCreateTime(new Date());
		passengerService.save(passenger);
		
		return ResponseMessage.ok();
	}
	
	/**
	 * 根据busno获取对应的班车信息。
	 * @param
	 * @return
	 * @author cjw 2020-02-26
	 */
	@RequestMapping("/getCompanyBusByBusno")
	public ResponseMessage getCompanyBusByBusno(String busno){
		//验证传来的参数
		try{
			busno=ObjectParse.parse_toStr_elseException(busno);
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!"+e.toString());
		}
		/**
		 * 根据busno获取对应的班车信息。
		 */
		CompanyBus companyBus=companyBusService.getCompanyBusByBusno(busno);
		if(companyBus==null){
			return ResponseMessage.error("未找到对应的班车信息!");
		}
		return ResponseMessage.ok(companyBus);
	}
	
	/**
	 * 分页获取乘客信息。
	 * @param
	 * @return 
	 * @author cjw 2020-02-25
	 */
	@RequestMapping("/getPassengerByPage")
	public ResponseMessage getPassengerByPage(Integer pageNum, Integer pageSize){
		//解析传来的参数
		MpUser mpUser=(MpUser) request.getAttribute("wxUser");
		try{
			pageNum=ObjectParse.parse_toInt_elseException(pageNum);
			pageSize=ObjectParse.parse_toInt_elseException(pageSize);
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!"+e.toString());
		}
		
		/**
		 * 分页获取乘客信息。
		 */
		Page<Passenger> page=passengerService.getPassengerByPage(mpUser.getIdcardno(), pageNum, pageSize);
		Page<Map<String,Object>> pageReturn=OutputCompanyBus.generatePassengerByPage(page);
		return ResponseMessage.ok(pageReturn);
	}
	
	/**
	 * 获取员工最新的乘车记录。
	 * @param
	 * @return
	 * @author cjw 2020-02-26
	 */
	@RequestMapping("/getLatestPassenger")
	public ResponseMessage getLatestPassenger(){
		//解析传来的参数
		MpUser mpUser=(MpUser) request.getAttribute("wxUser");
		
		Passenger passenger=passengerService.getLatestPassenger(mpUser.getIdcardno());
		Map<String,Object> mapPassenger=OutputCompanyBus.generatePassenger(passenger);
		return ResponseMessage.ok(mapPassenger);
	}
	
	









}
